package top.lshaci.learning.netty.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * MessageDecoder
 *
 * @author lshaci
 * @since 1.0.0
 */
public class MessageDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MessageDecoder decode...");
        // 将得到的二进制字节码转成 MessageProtocol
        final int len = in.readInt();
        byte[] content = new byte[len];
        in.readBytes(content);

        MessageProtocol messageProtocol = new MessageProtocol(len, content);
        out.add(messageProtocol);
    }
}
